package com.example.testtransition;


import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

//import com.yidui.base.log.Logger;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created by Loyea.com on 7月20日.
 */
public class RomUtils {
    private static final String TAG = "RomUtils";

    public class AvailableRomType {
        public static final int MIUI = 1;
        public static final int FLYME = 2;
        public static final int ANDROID_NATIVE = 3;
        public static final int NA = 4;
    }

    public static int getLightStatausBarAvailableRomType() {
        if (isMIUIV6OrAbove()) {
            return AvailableRomType.MIUI;
        }

        if (isFlymeV4OrAbove()) {
            return AvailableRomType.FLYME;
        }

        if (isAndroidMOrAbove()) {
            return AvailableRomType.ANDROID_NATIVE;
        }

        return AvailableRomType.NA;
    }

    //Flyme V4的displayId格式为 [Flyme OS 4.x.x.xA]
    //Flyme V5的displayId格式为 [Flyme 5.x.x.x beta]
    public static boolean isFlymeV4OrAbove() {
        String displayId = Build.DISPLAY;
        if (!TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) {
            String[] displayIdArray = displayId.split(" ");
            for (String temp : displayIdArray) {
                //版本号4以上，形如4.x.
                if (temp.matches("^[4-9]\\.(\\d+\\.)+\\S*")) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isFlymeV6OrAbove() {
        String displayId = Build.DISPLAY;
        if (!android.text.TextUtils.isEmpty(displayId) && displayId.contains("Flyme")) {
            String[] displayIdArray = displayId.split(" ");
            for (String temp : displayIdArray) {
                //版本号4以上，形如4.x.
                if (temp.matches("^[6-9]\\.(\\d+\\.)+\\S*")) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isMIUIV6OrAbove() {
        String miuiVersionCodeStr = getSystemProperty("ro.miui.ui.version.code");
        if (!android.text.TextUtils.isEmpty(miuiVersionCodeStr)) {
            try {
                int miuiVersionCode = Integer.parseInt(miuiVersionCodeStr);
                if (miuiVersionCode >= 4) {
                    return true;
                }
            } catch (Exception e) {
            }
        }
        return false;
    }

    public static boolean isMIUIV9OrAbove() {
        String miuiVersionCodeStr = getSystemProperty("ro.miui.ui.version.code");
        if (!android.text.TextUtils.isEmpty(miuiVersionCodeStr)) {
            try {
                int miuiVersionCode = Integer.parseInt(miuiVersionCodeStr);
                if (miuiVersionCode >= 7) {
                    return true;
                }
            } catch (Exception e) {
            }
        }
        return false;
    }

    //Android Api 23以上
    public static boolean isAndroidMOrAbove() {
        if (Build.VERSION.SDK_INT >= 23) {
            return true;
        }
        return false;
    }

    public static String getSystemProperty(String propName) {
        //低版本 8.0以下 机器会卡顿，采用文件读取方式
        if (Build.VERSION.SDK_INT >= 26) {
            return getSystemPropertyUp26(propName);
        } else {
            return getSystemPropertyDown26(propName);
        }
    }

    public static String getSystemPropertyUp26(String propName) {
        String line;
        BufferedReader input = null;
        try {
            Process p = Runtime.getRuntime().exec("getprop " + propName);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);
            line = input.readLine();
            input.close();
        } catch (IOException ex) {
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
//        Logger.d(TAG, "getSystemPropertyUp26 line == " + line);
        return line;
    }

    public static String getSystemPropertyDown26(String propName) {
        String line = "";
        try {
            BuildProperties properties = new BuildProperties();
            line = properties.getProperty(propName);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        Logger.d(TAG, "getSystemPropertyDown26 line == " + line);
        return line;
    }


    public static class BuildProperties {

        private final Properties properties;

        /**
         * 该方法读取只支持 8.0 以下，因为在 Android O (8.0)开始，
         * 对 /system/build.prop 的权限进行了限制
         */
        private BuildProperties() throws IOException {
            properties = new Properties();
            properties.load(new FileInputStream(new File(Environment.getRootDirectory(), "build.prop")));
        }

        public String getProperty(final String name) {
            return properties.getProperty(name);
        }

    }

}
